package cn.iocoder.yudao.boot.jpa.service.impl;

import cn.iocoder.yudao.boot.jpa.repository.BaseRepository;
import cn.iocoder.yudao.boot.jpa.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

public abstract  class BaseServiceImpl<T, ID extends Serializable, R extends BaseRepository<T, ID>>
        implements BaseService<T, ID> {
    @Autowired
    private R dao;


    @Override
    public void delete(ID id) {
        dao.deleteById(id);
    }

    @Override
    public void delete(Iterable<ID> ids) {
        dao.deleteAllById(ids);
    }

    @Override
    public Boolean save(T t) {
        dao.save(t);
        return true;
    }

    @Override
    public Boolean updateById(T t) {
        dao.save(t);
        return true;
    }

    @Override
    public Optional<T> get(ID id) {
        return dao.findById(id);
    }

    @Override
    public List<T> list(Iterable<ID> ids) {
        return dao.findAllById(ids);
    }

    @Override
    public List<T> list() {
        return dao.findAll();
    }

    @Override
    public Page<T> page(Integer page, Integer size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<T> page1 = dao.findAll(pageRequest);
        return page1;
    }
}